package com.nuaa.qianru.run;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nuaa.qianru.dao.TpDao;
import com.nuaa.qianru.model.AlarmInfo;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@Component  //注入容器
//监听rabbitmq,参数1:队列名称,参数2:是否持久化,参数3:是否独占,参数4:是否自动删除
@RabbitListener(queuesToDeclare = @Queue(value = "MessageCenter", durable = "true", exclusive = "false", autoDelete = "false"))
public class Customer {

    @Autowired
    private TpDao tpDao;

    private MqttServer5 mqttServer=MqttServer5.getInstance();
    private MqttServer6 mqttServer1=MqttServer6.getInstance();

    //消费消息
    @RabbitHandler //加上此注解后，会自动回调
    public void receive(byte[]  message) {
        String mess = null;
        try {
            mess = new String(message, "UTF-8");
            if (!mess.equals("")) {
                mess = mess.replaceAll("\r|\n","").replaceAll(" ","");
                mess = mess.replaceAll("\\\\","");
                mess = mess.substring(1,mess.length()-1);

                mess = mess.replaceAll("\""+"\\{","\\{");
                mess = mess.replaceAll("\\}"+"\"","\\}");
                mess = mess.replaceAll("\""+"\"","\"");
                mess = mess.replaceAll("\""+"\\[","\\[");
                mess = mess .replaceAll("\\]"+"\"","\\]");
//                System.out.println(mess);

                //物流部分的报警信息
                if(!mess.contains("[") && mess.contains(":{")){
                    JSONObject jsonObject = JSON.parseObject(mess);
                    JSONObject warn = jsonObject.getJSONObject("MessageContent");
                    String warnString = warn.getString("text");
                    if (!mess.contains("设备号")&&!mess.contains("面板")&&!mess.contains("总气源")&&!mess.contains("排屑")&&!mess.contains("反冲泵")&&!mess.contains("高压泵")&&!mess.contains("环喷泵")&&!mess.contains("低压泵")
                            &&!mess.contains("过滤")&&!mess.contains("水冷机")&&!mess.contains("液压站")&&!mess.contains("Z轴")&&!mess.contains("X轴")&&!mess.contains("Y轴")&&!mess.contains("Z轴")&&!mess.contains("刀库")
                            &&!mess.contains("刀长补")&&!mess.contains("看门狗")&&!mess.contains("铣削")&&!mess.contains("非法")&&!mess.contains("障碍物")&&!mess.contains("冷却液")&&!mess.contains("操作区防护门")) {

                        DateFormat format = new SimpleDateFormat("MM-dd HH:mm:ss");
                        format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
                        String time = format.format(new Date());
                        AlarmInfo alarmInfo = new AlarmInfo();
                        alarmInfo.setInfo(warnString);
                        alarmInfo.setLevel1("二级报警");
                        alarmInfo.setTime1(time);
                        tpDao.insertAlarm4(alarmInfo);
                        mqttServer.publishMessage("logistics", warnString, 1);
                    }else{

                        DateFormat format = new SimpleDateFormat("MM-dd HH:mm:ss");
                        format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
                        String time = format.format(new Date());
                        AlarmInfo alarmInfo = new AlarmInfo();
                        alarmInfo.setInfo(warnString);
                        alarmInfo.setLevel1("二级报警");
                        alarmInfo.setTime1(time);
                        tpDao.insertAlarm1(alarmInfo);
                        mqttServer1.publishMessage("machine", warnString, 1);
                    }
                }
//                if(mess.contains("[")){
//                    JSONArray messageContent = jsonObject.getJSONArray("MessageContent");
//                    if (messageContent.size() > 0) {
//                        for (int i = 0; i < messageContent.size(); i++) {
//                            JSONObject warnContent = messageContent.getJSONObject(i);
//                            String warn = warnContent.getString("text");
//                            AlarmInfo alarmInfo = new AlarmInfo();
//                            alarmInfo.setInfo(warn);
//                            alarmInfo.setLevel1("二级报警");//自己设置报警等级
//                            alarmInfo.setTime1(time);
//                            tpDao.insertAlarm4(alarmInfo);
//                            mqttServer.publishMessage("logistics", warn, 1);
//                        }
//                    }
//                }
//                }else{
//                    JSONObject warnObject = jsonObject.getJSONObject("MessageContent");
//                    String warn = warnObject.getString("text");
//                    AlarmInfo alarmInfo = new AlarmInfo();
//                    alarmInfo.setId(i);
//                    alarmInfo.setInfo(warn);
//                    alarmInfo.setLevel1("二级报警");//自己设置报警等级
//                    alarmInfo.setTime1(time);
//                    i++;
//                    tpDao.insertAlarm4(alarmInfo);
//                    mqttServer.publishMessage("logistics",warn,1);
//                }
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
}

